package com.laker.autotest.api.testcase;

import cn.hutool.json.JSONUtil;
import com.laker.autotest.api.core.EasyOkHttpClient;
import com.laker.autotest.api.service.JsonPlaceholderUserService;
import com.laker.autotest.api.service.ReqresUserService;
import com.laker.autotest.api.service.UserRequest;
import com.laker.autotest.api.service.UserResponse;
import okhttp3.Response;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.io.IOException;

@Test(suiteName = "api")
public class UserServiceTest {

    private static final String NAME = "Laker";
    private static final String JOB = "Engineer";
    private static int createdUserId;

    private ReqresUserService reqresUserService;
    private JsonPlaceholderUserService jsonPlaceholderUserService;

    @BeforeClass
    public void setUp() {
        reqresUserService = new ReqresUserService();
        jsonPlaceholderUserService = new JsonPlaceholderUserService();
    }

    @Test(priority = 1)
    public void test_JsonPlaceholder_CreateUser() throws IOException {
        UserRequest user = new UserRequest(NAME, JOB);
        Response response = jsonPlaceholderUserService.createUser(user);

        Assert.assertEquals(response.code(), 201); // Created

        UserResponse userResponse = EasyOkHttpClient.parseResponse(response, UserResponse.class);
        Assert.assertEquals(userResponse.getName(), NAME);
        Assert.assertEquals(userResponse.getJob(), JOB);
        Assert.assertNotNull(userResponse.getId());

        createdUserId = userResponse.getId();
    }

    @Test(priority = 2)
    public void test_Reqres_GetUserById() throws IOException {
        Response response = reqresUserService.getUserById(createdUserId);
        Assert.assertEquals(response.code(), 200);

        UserResponse userResponse = EasyOkHttpClient.parseResponse(response, UserResponse.class);
        Assert.assertEquals(userResponse.getId().intValue(), createdUserId);
        Assert.assertEquals(userResponse.getName(), NAME);
    }

    @Test(priority = 3)
    public void test_Reqres_UpdateUser() throws IOException {
        UserRequest updatedUser = new UserRequest("LakerUpdated", "Senior Engineer");
        Response response = reqresUserService.updateUser(createdUserId, updatedUser);
        Assert.assertEquals(response.code(), 200);

        UserResponse userResponse = EasyOkHttpClient.parseResponse(response, UserResponse.class);
        Assert.assertEquals(userResponse.getName(), "LakerUpdated");
        Assert.assertEquals(userResponse.getJob(), "Senior Engineer");
    }

    @Test(priority = 4)
    public void test_Reqres_DeleteUser() throws IOException {
        Response response = reqresUserService.deleteUser(createdUserId);
        Assert.assertEquals(response.code(), 200); // 注意：reqres.in 删除返回是 200 而不是 204
    }

    @Test
    public void test_JsonPlaceholder_GetAllUsers() throws IOException {
        Response response = jsonPlaceholderUserService.getAllUsers();
        Assert.assertEquals(response.code(), 200);

        String responseBody = response.body().string();
        // 可以解析为数组或列表
        UserResponse[] users = JSONUtil.toList(JSONUtil.parseArray(responseBody), UserResponse.class).toArray(new UserResponse[0]);
        Assert.assertTrue(users.length > 0);
    }
}